Conditional Statements
1. Comparison Operators
Comparison operators are used to compare values. They evaluate to True or False depending on the values you compare.
We can't use a single = sign for comparison because it is reserved for assignment. The comparison operators are:
==Equal to!=Not equal to<Less than>Greater than<=Less than or equal to>=Greater than or equal to
x, y = 3, 5
print(x == y) # Output: False
2. Conditional Statements
1. if
if condition:
# code to execute if condition is True
2. if - else
if condition:
# code if condition is True
else:
# code if condition is False
3. if - elif - else
if condition1:
# code if condition1 is True
elif condition2:
# code if condition2 is True
else:
# code if all conditions are False
age = 25
has_id = True
if age >= 21 and has_id:
print("You can enter the bar and drink.")
elif age >= 18 and has_id:
print("You can enter the bar but can't drink.")
else:
print("You can't enter the bar.")